/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.bath.domains.prey;

/**
 *
 * @author pjl
 */
public class Line {

    private float kx;
    private float ky;
    private float cx;
    private float cy;

    public Line(MyPoint p1, MyPoint p2) {
        kx = p2.x - p1.x;
        ky = p2.y - p1.y;

        cx = p1.x;
        cy = p1.y;
    }

    public Float intersectsLineAt(Line l) {
        double kk=l.ky*kx-l.kx*ky;
        double cc=l.kx*(cy-l.cy)-l.ky*(cx-l.cx);
        return new Float(cc/kk);
    }

    public boolean intersectsWith(Line l) {
        float t=intersectsLineAt(l);
        if (t<0.0 || t>1.0) return false;
        t=l.intersectsLineAt(this);
        if (t<0.0 || t>1.0) return false;
        return true;
    }

    public static void main(String [] arg) {
        MyPoint o=new MyPoint(0,0);
        MyPoint a=new MyPoint(10,10);
        MyPoint b=new MyPoint(1,0);
        MyPoint c=new MyPoint(0,1);

        MyPoint d=new MyPoint(1,0);
        MyPoint e=new MyPoint(11,10);

        Line oa=new Line(o,a);
        Line ao=new Line(a,o);
        Line ob=new Line(o,b);
        Line oc=new Line(o,c);
        Line ab=new Line(a,b);
        Line ac=new Line(a,c);
        Line bc=new Line(b,c);
        Line de=new Line(d,e);

        System.out.println(" oa - ob (0) " + oa.intersectsLineAt(ob));
        System.out.println(" ao - ob (1) " + ao.intersectsLineAt(ob));
        System.out.println(" oa - bc (0.05) " + oa.intersectsLineAt(bc));
        System.out.println(" ao - bc (0.95) " + ao.intersectsLineAt(bc));
        System.out.println(" oa - de (inf) " + oa.intersectsLineAt(de));
        System.out.println(" ao - de (inf) " + ao.intersectsLineAt(de));



    }
}
